#!/bin/bash
MYSQL_CONTEXT="mysql "

function restart_slave()
{
    echo "stop slave " | ${MYSQL_CONTEXT}
    echo "start slave " | ${MYSQL_CONTEXT}
    exit 1
}

echo "show slave status \G" | ${MYSQL_CONTEXT} > _slave_tmp
cat _slave_tmp |grep Slave_IO_Running|grep Yes > /dev/null
if [ $? -ne 0 ]; then
    restart_slave
fi

cat _slave_tmp |grep Slave_SQL_Running|grep Yes > /dev/null
if [ $? -ne 0 ]; then
    restart_slave
fi

cur_info=''
tp_info=`cat _slave_tmp |grep Master_Log_File|awk '{print $2}'`
cur_info="$cur_info $tp_info"

tp_info=`cat _slave_tmp |grep Read_Master_Log_Pos|awk '{print $2}'`
cur_info="$cur_info $tp_info"

tp_info=`cat _slave_tmp |grep Exec_Master_Log_Pos|awk '{print $2}'`
cur_info="$cur_info $tp_info"

if [ -f _last_slave_pos ]; then
    last_info=`cat _last_slave_pos`
    if [ "$cur_info" == "$last_info" ]; then
       restart_slave 
    fi
fi
echo $cur_info > _last_slave_pos

